package com.witframework.baseapp.security.controller;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.witframework.baseapp.security.BaseUser;
import com.witframework.baseapp.security.BaseUser;
import com.witframework.core.WitFrameWork;
import com.witframework.web.mvc.WebContext;

@Component
public class ChangePasswordController {	
	@Transactional
	public String executor(WebContext webContext) {
		if(webContext.getParameter("changepassword")!=null){
			BaseUser u=webContext.getUserSession().getUser();
			String password=webContext.getParameter("oldpassword");
			if(!u.getPassword().equals(password)){
				webContext.setPageError("原密码错误！");
			}else{
				String newPassword=webContext.getParameter("password");
				u.setPassword(newPassword);
				WitFrameWork.getSimpleBaseService(BaseUser.class).update(u);
				webContext.getHttpRequest().getSession().removeAttribute("UserSession");		
				//return "redirect:"+webContext.getContextPath();
				return "classpath:/com/witframework/webroot/framework/security/changepassword.jsp?code=1";
			}
		}
		return "classpath:/com/witframework/webroot/framework/security/changepassword.jsp";
	}
	
}
